package com.boot.security.server.utils.mffl;

import com.boot.security.server.dao.SysSmsDao;
import com.boot.security.server.utils.SmsUtils;
import com.github.wxpay.sdk.WXPayUtil;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.Yaml;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;


/**
 * 短信发送类
 *
 * @author liyuting
 * @date 2020/1/13
 */
public class NewSmsUtils {
    private String userid;
    private String account;
    private String password;
    private String sendTime;
    private String action;
    private String extno;
    private String sign;
    private String url;

    public NewSmsUtils() {
        try {
            Yaml yaml = new Yaml();
            InputStream yamlinputstream = Thread.currentThread().getContextClassLoader().getResourceAsStream("application-prod.yml");
            Map<String, Object> map = yaml.loadAs(yamlinputstream, Map.class);
            userid = ((Map<String, Object>) map.get("sms")).get("userid").toString();
            account = ((Map<String, Object>) map.get("sms")).get("account").toString();
            password = ((Map<String, Object>) map.get("sms")).get("password").toString();
            action = ((Map<String, Object>) map.get("sms")).get("action").toString();
            sign = ((Map<String, Object>) map.get("sms")).get("sign").toString();
            url = ((Map<String, Object>) map.get("sms")).get("url").toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Map<String, String> sendSms(SysSmsDao smsDao, String phone, String content, String code) throws Exception {
        //String httpurl = ""+url+"?action=" + action + "&userid=" + userid + "&account=" + account + "&password=" + password + "&mobile="+phone+"&content=" + sign+content + "&sendTime=" + sendTime + "&extno=" + extno + "";
        String httpurl = "" + url + "?action=" + action + "&userid=" + userid + "&account=" + account + "&password=" + password + "&mobile=" + phone + "&content=" + sign + content + "";
        String s = doGetString(httpurl);
        Map<String, String> response = WXPayUtil.xmlToMap(s);
        SmsUtils.addSms(smsDao, phone, content, code, response.get("message"), response.toString());
        return response;
    }

    //get请求参数
    public String doGetString(String url) {
        String result = "";
        HttpClient httpclient = HttpClients.createDefault();
        HttpGet httpget = new HttpGet(url);
        try {
            HttpResponse httpresponse = httpclient.execute(httpget);
            HttpEntity entity = httpresponse.getEntity();
            if (entity != null) {
                result = EntityUtils.toString(entity, "UTF-8");
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            httpget.releaseConnection();
        }
        return result;
    }
}
